Submit disabled Dropdown,disableddropdown

來源:互聯網
上載者:User

Submit disabled Dropdown,disableddropdown
需求:完成一個不可以改的dropdown,(符合一定logic)在js中設value,並且提交表單時把值提交到server


1.嘗試readonly


<script src="jquery-1.11.1.min.js"></script><form name="frm" method="post"><select id="sel" name="sel"><option value="1" >1</option><option value="2">2</option><option value="3">3</option></select><input type="submit" value="submit" /></form><script>$(document).ready(function(){$("#sel").val(2);$("#sel").attr("readonly","readonly");});</script>




結果發現,readonly對dropdown是無效的,使用者依然可以選,我們必須disable掉它。


2.嘗試disable


<script src="jquery-1.11.1.min.js"></script><form name="frm" method="post"><select id="sel" name="sel"><option value="1" >1</option><option value="2">2</option><option value="3">3</option></select><input type="submit" value="submit" /></form><script>$(document).ready(function(){$("#sel").val(2);$("#sel").attr("disabled","disabled");});</script>



結果發現,提交表單時,html form 並沒有帶上disable的成員







現在disable拿掉,可以看到html form中包含了dropdown的值




3.依然disable,但是提交表單時把disable拿掉


html不變,script加一句變為:


$(document).ready(function(){ $("#sel").closest("form").submit(function () {                $("#sel").removeAttr("disabled");            });$("#sel").val(2);$("#sel").attr("disabled","disabled");});




可以看到,這樣就實現了需求。



當然,還可以通過hidden的方式來實現同樣的事情,即在dropdown值改變的時候更新hidden。
使用了<input type="submit" value="Submit" onclick="thisdisabled=true" />不可以提交

看看我的例子吧

<HTML>
<HEAD>
<TITLE> New Document </TITLE>

<script>
function goo(obj)
{
obj.disabled =true;
document.getElementById("form1").submit();
}
</script>
</HEAD>

<BODY>
<form action="1.asp" method="post" id="form1">
<input type=button value="提交" onclick="goo(this);">
</form>
</BODY>
</HTML>
 
html submit 的 disabled 屬性 怎取到JS的Boolean值

你是想讓提交按鈕提交後就不能在點了是吧var b=true;function sub(){var su=document.getElementById("sub");if(b){ su.disabled=true; b=false;}else{su.disabled=false;}}<input id="sub" type="submit" value="提交" ${ b? "disabled":"" } onclick="sub();">
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.