选择选项值时,如何清除或更改输入字段的文本?

时间:2018-01-20 22:00:35

标签: jquery html5

我有这个HTML5代码:

$("#list").on("change", function () {
 if(this.value === "Orange") {
 $('#price').val('');
}
});

并且使用下面的代码,当我选择" Orange"时,我试图清除输入字段的默认值。下拉列表中的选项。

public class Main{

public static int counter=0;

public static void main(String[] args) {

    Thread thread[] = new Thread[10]; //10 threads created

    for(int i=0;i<10;i++){
        thread[i]=new Thread(new NewThread());
        thread[i].start();
    }

}

public static int increment(){

    long count=0;

    count=++counter;

    if(count>1000000000){
        return -1;
    }

    return counter;
}

}


public class NewThread implements Runnable{

    Lock lock=new ReentrantLock();

    public boolean isPrime(int number) throws ThresholdReachedException{

        if(number<0){
            throw new ThresholdReachedException("Limit Reached");
        }

        if(number==1){
            return false;
        }

        double limit=Math.pow(number, 0.5);

        for(int i=1;i<(int)limit;i++){
            if(number%i==0){
                return false;
            }
        }
        return true;
    }

    @SuppressWarnings("static-access")
    @Override
    public void run() {

        //System.out.println(Thread.currentThread().getName()+" running");
        int number=0;
        try{
            while(true){
                if(lock.tryLock()){
                //lock.lock();
                number=Main.increment();
                lock.unlock();
                }else{
                    Thread.currentThread().sleep(2000);;
                }

                if(isPrime(number)){
                    System.out.println(Thread.currentThread().getName()+" Prime number : "+number);
                }
            }
        }catch(ThresholdReachedException e){

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

    }

}

我无法理解为什么这不起作用!我错过了什么?

react-weather

2 个答案:

答案 0 :(得分:0)

$("#list").on("change", function() {
  var dataSelect = $("#list option:selected").attr('data-value');
  if(dataSelect == '4') $('#price').html('');
});

telegram bot api

答案 1 :(得分:0)

使用 data() attr() jQuery方法提取data-*值。

在演示中评论的详细信息

演示

&#13;
&#13;
// A range of prices
var priceIndex = [2.26, 5.5, 10, 20.01];

// change event on select#list...
$("#list").on("change", function() {

  /* Get value from data-* attribute with data()
  || method and store the value of the selected
  || option.option in a variable
  */
  var val = $(this).find('option:selected').data('value');
  
  // Determine the price by matching val to index
  var v = priceIndex[val-1];
  
  // Format price
  var price = currency(v, 'us');
  
  // Display value in output#price
  $('#price').val('$'+price);

});

// Utility to format currency
function currency(number, country) {
    return (number).toLocaleString(country, {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
    });
}
&#13;
<select id="list">
<option default></option>
<option data-value="1" class="option">Red</option>
<option data-value="2" class="option">Green</option>
<option data-value="3" class="option">Blue</option>
<option data-value="4" class="option">Orange</option>
</select>

<label>&nbsp;&nbsp;<output id="price">0</output></label>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;