2015-09-08 34 views
0

這是我使用jQuery的腳本的一部分。腳本中的jQuery attr和prop組合

 jQuery(".disabableInputField").addClass("disabledInputField"); 
     cstaIpVal[0] = jQuery("#private-circuit-to-csta-subnet").val(); 
     cstaIpVal[1] = jQuery("#private-circuit-to-csta-subnet\\.netmask").val(); 
     jQuery("#private-circuit-to-csta-subnet\\.netmask").val(""); 
     jQuery("#private-circuit-to-csta-subnet").val(""); 
     jQuery(".disabableInputField").attr("disabled",true);   
     jQuery("#private-circuit-to-csta-subnet\\.netmask").prop("path", ""); 
     jQuery("#private-circuit-to-csta-subnet").prop("path",""); 

問題是,爲什麼調用prop和attr的組合不起作用。如果我撥打attr("disabled",true),那麼屬性集prop("path","")不起作用。當我切換訂單時,屬性設置不起作用。我無法通過prop實現設置禁用的屬性。我怎樣才能結合attr和道具呼叫?或者我如何通過prop調用設置html元素的禁用屬性?

謝謝。

+0

你濫用這兩種方法。 'prop'用於'disabled','attr'用於定製屬性('path') – hindmost

回答

1

我想這是因爲濫用方法。使用「prop」設置屬性,使用「attr」設置屬性。不是反之。以下工作適合我。

jQuery(".disabableInputField").prop("disabled", true); 

參見:https://learn.jquery.com/using-jquery-core/faq/how-do-i-disable-enable-a-form-element/

也許這樣的:

<input type="text" id="test" /> 
<script type="text/javascript"> 
    jQuery("#test").prop("disabled",true); 
    jQuery("#test").attr("path", ""); 
</script> 
<!-- result --> 
<input disabled="" id="test" type="text" path=""> 
+0

更改後的代碼: 'jQuery(「#private-circuit-to-csta-subnet \\ .netmask」)prop 「禁用」,TRUE); 。jQuery的( 「#私人電路到CSTA子網」)丙( 「禁用」,TRUE); jQuery(「#private-circuit-to-csta-subnet \\ .netmask」)。attr(「path」,「」); jQuery(「#private-circuit-to-csta-subnet」)。attr(「path」,「」);' path is correct,it is empty,but disabled attribute does not work,it's still a editable field :/我現在很困惑 –

+0

請顯示您的html標記爲#private-circuit-to-csta-subnet – unconnected

+0

'' 它無法更改,我需要使用此版本 謝謝你的幫助。 –