2014-06-16 37 views
-1

我正在一個網頁上有約8個HTML控件和一個HTML按鈕,使用它在8個HTML控件中的數據提交後點擊按鈕。 我想一旦按鈕使用JavaScript單擊以清除所有的8場,我有以下腳本onClick事件不工作後點擊一個HTML按鈕

HTML頁面

<td> 
    <select id="cbxProduct" data-bind="options: $root.productNames, optionsText: 'ProductName', optionsValue: 'ProductName', value: selectedChoice, optionsCaption: 'Product'"> 
    </select> 
</td> 
<td> 
    <select id="cbxTerm" data-bind="options: $root.termNames, optionsText: 'TermName', optionsValue: 'TermName', enable: selectedChoice, value: selectedTerm, optionsCaption: 'Term'"> 
    </select> 
</td> 
<td> 
    <input type="text" id="txbBVolume" placeholder="Bid Volume" data-bind="value:  bidVolume" /> 
</td> 
<td> 
    <select id="cbxBCP" data-bind="options: $root.counterpartyNames, optionsText: 'CounterPartyName', optionsValue: 'CounterPartyName', value: selectedBidCounterParty, optionsCaption: 'Bid CP'"> 
    </select> 
</td> 
<td> 
    <input type="text" id="txbB" placeholder="Bid" data-bind="value: bid" /> 
</td> 
<td> 
    <input type="text" id="txbO" placeholder="Offer" data-bind="value: offer" /> 
</td> 
<td> 
    <select id="cbxOCP" data-bind="options: $root.counterpartyNames, optionsText: 'CounterPartyName', optionsValue: 'CounterPartyName', value: selectedOfferCounterParty, optionsCaption: 'Offer CP'"> 
    </select> 
</td> 
<td> 
    <input type="text" id="txbOVolume" placeholder="Offer Volume" data- bind="value: offerVolume" /> 
</td> 

<td > 
    <input type="button" class="btn btn-success" data-bind="  click: clear" value="Add New Entry" /> 
</td> 
@section scripts { 
<script src="~/App/ClearAllFields.js"></script> 
} 

淘汰賽JS

self.add = function (cc) { 
    var payload = { 
     Term: this.selectedTerm(), Product: this.selectedChoice(), 
     BidCP: this.selectedBidCounterParty(), BidVolume: (this.bidVolume() == "") ? null : this.bidVolume(), Bid: (this.bid() == "") ? null : this.bid(), Offer: (this.offer() == "") ? null : this.offer(), 
     OfferVolume: (this.offerVolume() == "") ? null : this.offerVolume(), OfferCP: this.selectedOfferCounterParty(), Locked: false, Sequence: "", TermID: 0, ProductID: 0 
    }; 
    $.ajax({ 
     url: '/odata/CC', 
     type: 'POST', 
     // data: ko.toJSON(payload), 
     data: JSON.stringify(payload), 
     contentType: 'application/json', 
     dataType: 'json' 
    }); 

    this.selectedChoice("Product"); 
    this.selectedTerm("Term"); 
    this.selectedBidCounterParty("Bid CP"); 
    this.bVolume(""); 
    this.bid(""); 
    this.offer(""); 
    this.oVolume(""); 
    this.selectedOfferCounterParty("Offer CP"); 
} 

我以爲一切都很好它清除TextBox字段但不清除下拉框。

例如,我已經通過點擊"Add new entry"按鈕提交product=abc, term=fall, bVolume=5, BCP=X, Bid=10, Offer=12, OCP=C, OVolume=6有一次我按一下按鈕,它提交的數據,並清除所有的文本字段bVolume,投標,報價,oVolume並留下下拉框,因爲它們是

+0

@ j08691是我不好,我已經編輯,現在的問題 – DoIt

+0

找不到任何理由反對但不介意他們,如果留下評論是反對票的話,會有幫助。 – DoIt

+0

難道是'data-bind ='點擊:添加''覆蓋'onclick'屬性? –

回答

1

變化this.selectedChoice("Product");this.selectedChoice("");

順便說一句,從控件中刪除所有的ID屬性,因爲:

  1. 你不需要他們。
  2. 這是違反KnockoutJS原則。
  3. 你不需要它們。 :)

編輯:

變化this.selectedChoice("Product");this.selectedChoice(null);

工作演示:http://jsfiddle.net/DianaNassar/e35aU/1/

+0

我試過了你也說過的方式,但沒有工作和關於Id的,早些時候我正在使用JavaScript並使用它們 – DoIt

+0

它是否聲明爲this.selectedChoice = ko.observable();首先? –

+0

是的,它不是我不能提交任何東西,我的意思是沒有聲明 – DoIt

0

您需要添加值綁定:

<select id="cbxProduct" data-bind="options: $root.productNames, value: selectedChoice"> 
</select> 

下面是如何重置下拉菜單的快速演示:

http://jsfiddle.net/4hVf4/

+0

對不起,我已經在使用價值綁定對下拉列表 – DoIt

+0

@Dev我已經添加了一個小演示如何做它 –

+0

沒有看到你的完整代碼,很難看出它爲什麼不起作用。你確定你需要使用optionsValue和optionsText嗎?你能否提供完整的視圖模型代碼? –