带有jQuery的DOM下拉列表

时间:2016-11-13 18:18:49

标签: javascript jquery html

我不是高级jQuery开发人员,所以我在StackOverflow上问这个问题。

我有一个付款下拉列表(比特币,PayPal)。用户可以选择一个选项:比特币或PayPal。

如果用户选择bitcoin,则input元素的占位符将更改为bitcoin addressbitcoin value将为disabled

如果用户选择PayPal,则输入元素的占位符将更改,并且将启用Paypal值

我在jQuery中忘记了一些事情。但我不知道:



$(document).ready(function() {
    $('#example').click(function() {
        var payment = $("#example").val();
        $("#textarea").attr('placeholder', payment+' Address');

        if(("#example").val() == 'Bitcoin') {
            $("#textare").attr('placeholder','bitcoin');
        }
        if(("#example").val() == 'Paypal'); {
            $('#textare').attr('placeholder','paypal');
        }
    });
});

.ciao { background-color: red; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="example">
    <option value="Paypal">Paypal</option>
    <option value="Bitcoin">Bitcoin</option>
</select>
<input type="text" class="form-name"  placeholder="Name">
<input type="text" class="form-email" placeholder="E-mail">
<input type="text" class="" id="textarea">
<input type="text" class="form-" id="textare" placeholder="Your Ammont">
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

只需在if(("#example").val() == 'Bitcoin') {前面添加$。

所以它应该是if($("#example").val() == 'Bitcoin') {

&#13;
&#13;
$(document).ready(function() {
    $('#example').click(function() {
        var payment = $("#example").val();
        $("#textarea").attr('placeholder', payment+' Address');

        if($("#example").val() == 'Bitcoin') {
            $("#textare").attr('placeholder','bitcoin');
        }
        if($("#example").val() == 'Paypal'); {
            $('#textare').attr('placeholder','paypal');
        }
    });
});
&#13;
.ciao { background-color: red; }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="example">
    <option value="Paypal">Paypal</option>
    <option value="Bitcoin">Bitcoin</option>
</select>
<input type="text" class="form-name"  placeholder="Name">
<input type="text" class="form-email" placeholder="E-mail">
<input type="text" class="" id="textarea">
<input type="text" class="form-" id="textare" placeholder="Your Ammont">
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你有

的问题
if(("#example").val() = 'Bitcoin'); {

if语句的语法是

if(true) { /*do something*/ } 

此外,当您使用if比较某些变量时,语法应为==而不是===是比较运算符,=表示等于。以下是工作示例:

&#13;
&#13;
var example = $('#example');
var textarea = $("#textarea");
var textare = $("#textare");

$(document).ready(function() {
  example.on('click', function() {
    var payment = example.val();
    textarea.attr('placeholder', payment+' Address');
  
    if( example.val() == 'Bitcoin' ) {
      textare.attr('placeholder','bitcoin');
      textare.attr('disabled', 'true');
    }
  
    if( example.val() == 'Paypal') {
      textare.attr('placeholder','paypal');           
    }
  });
});
&#13;
.ciao {
  background-color: red; }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="example">
  <option value="Paypal">Paypal</option>                   <option value="Bitcoin">Bitcoin</option>
</select>
<input type="text" class="form-name"  placeholder="Name">
<input type="text" class="form-email" placeholder="E-mail">
<input type="text" class="" id="textarea">
<input type="text" class="form-" id="textare" placeholder="Your Ammont">
&#13;
&#13;
&#13;