2014-04-24 46 views
0

我有一個複選框。它將更新作爲輸入文本框的$('#express')的值。如果我使用.toggle而不是.click,該複選框將被隱藏。jQuery切換刪除輸入類型複選框

切換是我的複選框被刪除或隱藏的原因嗎?

HTML

<form id="addupload" method="post" action="order_history/lightbox/mini-upload-form/upload.php" enctype="multipart/form-data"> 
    <div id="upload"> 
     <div id="drop"> 
     Drop Here 
    <a>Browse</a> 
    <input id="uplo" type="file" name="upl" /> 
      </div> 
    <ul> </ul> 
    </div> 
    <div id="formcontent"> 
     <label class="required" for="unitprice" title="Unit price"><input type="text" id="unitprice" name="unitprice" />Unit price</label> 
    <label class="required" for="qty" title="How many pcs"><input type="text" id="qty" name="qty" />Quanity</label> 
    <label class="required" for="express" title="Express within China"><input type="text" id="express" name="express" />Express</label> 
    <label class="required" for="linkURL" title="Insert a full URL http:\\"><input type="text" id="linkURL" name="linkURL" />Link</label> 
     <label for="yourdesc" title="Describe your need clearly">Description<textarea id="yourdesc" name="yourdesc"></textarea></label> 
      <label for="shunfeng" title="SF is RMB25 for 1kg ">Express ?<input type="checkbox" id="shunfeng" class="shunfeng" name="shunfeng" /></label> 
      <label for="formsubmit" class="nocontent" id="uptest"><input type="button" id="submitButton" href="#" class="progress-button" onClick="upload()" value="Add to order" /><strong>Note:</strong> Items marked <img src="order_history/images/required.jpg" alt="Required marker" width="20" height="20" /> are required fields</label> 
    </div> 
</form> 

JQUERY

$("#shunfeng").toggle(
function(){ 
$('#express').val("25"); 
}, function(){ 
    $('#express').val("0"); 
    } 
); 

CSS

.shunfeng{ 
width:15px; 
height:15px;} 
+2

'toggle()'的形式在jQuery 1.8中被棄用,並在1.9中被刪除。你在使用更高版本嗎? –

+1

它從屏幕隱藏元素,並從帖子中刪除數據並獲取請求。 –

+0

至少在你的HTML代碼中沒有任何id##express# –

回答

3

功能$.toggle(fn, fn);您使用的是在棄用的jQuery 1.8的jQuery 1.9

刪除而且我認爲你需要change事件

$("#shunfeng").change(function() { 
    if(this.checked) { 
     $('#express').val("25"); //If checked set value to 25 
    } else { 
     $('#express').val("0"); //If unchecked set value to 0 
    } 
}); 
+0

你的腳本爲#express添加了25值,但是在更改後它不會添加0我正在使用jquery-1.10.2.js –

+1

@ user3150692,請參閱http://jsfiddle.net/47WLv/我想要這個嗎? – Satpal

+1

是的,但奇怪的是,它不適用於我,它增加了25,但不加0時,未選中 –

1

希望你有一個ID快遞DOM元素。

它工作正常使用jQuery 1.8

這裏是工作提琴。

click here

看的原因可能是因爲jQuery的你進口。有沒有控制檯錯誤?這將有助於.. 如果其jQuery 1.9嘗試使用更改而不是切換。

$("#shunfeng").change(
function(){ console.log("A"); 
$('#express').val("25"); 
}, function(){ 
    $('#express').val("0"); 
    } 
); 
+0

它不會設置爲0我正在使用jquery-1.10.2.js –

+0

看到小提琴.. –