两个文本框相等时禁用文本框

时间:2016-08-06 06:06:53

标签: javascript jquery

如果选中(文本框名称)和数量(文本框名称)相等,我如何禁用文本框。我使用这个代码工作,但当选择(文本框名称)等于3时,txtCombo(文本框名称)未被禁用,但当等于4时,它变为禁用我应该如何处理?提前谢谢你。

// disable button if equal
// $('#button').click(function(){
$('#button').click(function() {
  var firstValue = $("#quantitytotransfer").val();
  var secondValue = $("#selected").val();
  if ((firstValue == secondValue)) {
    $("#txtCombo").prop("disabled", true);
  }
});

function addCombo() {

  var textb = document.getElementById("txtCombo");
  var combo = document.getElementById("combo");
  var option = document.createElement("option");
  option.text = textb.value.trim();
  option.value = textb.value.trim();
  option.selected = true;

  if ($('#combo option[value="' + textb.value.trim() + '"]').text() == textb.value.trim()) {
    alert("Duplicate found or you entered empty value");
    return false;
  }
  try {
    combo.add(option, null); //Standard 
  } catch (error) {
    combo.add(option); // IE only
  }
  textb.value = "";
}



$("#txtCombo").on("keydown", function(e) {
  return e.which !== 32;
});




$(document).ready(function() {

  $('#button').click(function() {

    var data = [];
    $.each($("#combo option:selected"), function() {
      data.push($(this).attr("value"));

    });
    $('#imei').val(data.join(","));;
    var count = $("#combo :selected").length;
    $('#selected').val(count);


  });

});


$(document).ready(function() {

  $('#button').click(function() {

    var data = [];
    $.each($("#combo option:selected"), function() {
      data.push($(this).attr("value"));

    });
    $('#imei').val(data.join(","));;
    var count = $("#combo :selected").length;
    $('#selected').val(count);


  });

});


$("#combo").on('change', function() {

  var count = $("#combo :selected").length;
  $('#selected').val(count);


});
var text = $("#text").val();

var previousOption;
$('select[name=combo] option').each(function() {
  if (this.text == previousOption) $(this).remove();
  previousOption = this.text;
});



// separated by comma to textbox
$(document).ready(function() {

  $("#combo").change(function() {
    var data = [];
    $.each($("#combo option:selected"), function() {
      data.push($(this).attr("value"));
    });
    $('#imei').val(data.join(","));;

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<BODY style="font-family: sans-serif">

  <fieldset>
    <legend>Combo box</legend>
    Add to Combo:
    <input type="text" name="txtCombo" id="txtCombo" />Selected:
    <input type="text" name="selected" id="selected" />IMEI Selected:
    <input type="text" name="imei" id="imei" />Quantity:
    <input type="text" name="quantity" value="3" id="quantitytotransfer" />
    <br>

    <input type="button" id="button" value="Add" onclick="addCombo()">
    <br/>Combobox:
    <select name="combo" multiple id="combo"></select>
  </fieldset>
</BODY>

2 个答案:

答案 0 :(得分:0)

您可以轻松实现这一目标:

function checkIfEqual(){
    if($("#selected").val() === $("#quantitytotransfer").val())
        $("#txtCombo").prop("disabled", true);
    else
        $("#txtCombo").prop("disabled", false);
}

$("#button").click(function(){
    $("#selected").val($("#combo option:selected").length);
    checkIfEqual();
});

demo here

答案 1 :(得分:0)

你试过这个:

function addCombo() {
  var txtSelected = $('#selected').val();
  var qty = $('#quantitytotransfer').val();
  if(parseInt(txtSelected) == parseInt(qty)) {
    alert('Equal');  
    document.getElementById('txtCombo').disabled = true;
  } else {
    alert('Not equal');
    document.getElementById('txtCombo').disabled = false;
  } 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<BODY style="font-family: sans-serif">
    <fieldset>
        <legend>Combo box</legend>
        Add to Combo: <input type="text" name="txtCombo" id="txtCombo"/><br>
        Selected: <input type="text" name="selected" id="selected" class="toggle"/><br>
        IMEI Selected: <input type="text" name="imei" id="imei"/><br>
        Quantity: <input type="text" name="quantity" value="3" id="quantitytotransfer" class="toggle"/><br>
        <input type="button" id="button" value="Add" onclick="addCombo()"><br>
       Combobox: <select name="combo" multiple id="combo"></select>
    </fieldset>
</BODY>