如何清除readonly

时间:2016-04-06 19:02:19

标签: jquery readonly-attribute

我需要根据国家/地区为表单输入添加前缀,并防止编辑该前缀。我设置它是正常的,但是当我切换国家时,块(只读)没有清除。输入清除但前三列仍然只读。我在这里搜索了线程,他们大多都说使用attr(“readonly”,false)语句,但由于某种原因这不起作用。有人请指出问题吗?

//Populate Favored Class Options Table
    TableLayout tableFC = (TableLayout) findViewById(R.id.alchemist_favored_class);
    String[] fcr = getResources().getStringArray(R.array.alchemist_favored_class_race);
    String[] fcd = getResources().getStringArray(R.array.alchemist_favored_class_detail);

    TableRow.LayoutParams lp1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);
    TableRow.LayoutParams lp2 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);

    for (int i = 0; i < fcr.length; i++){

        TableRow tr = new TableRow(this);
        TextView tvFCR = new TextView(this);
        TextView tvFCD = new TextView(this);

        tr.addView(tvFCR);
        tvFCR.setText(fcr[i]);
        tvFCR.setSingleLine(true);
        tvFCR.setPadding(2,2,2,2);
        tvFCR.setLayoutParams(lp1);

        tr.addView(tvFCD);
        tvFCD.setText(fcd[i]);
        tvFCD.setPadding(2,2,2,2);
        tvFCD.setLayoutParams(lp2);

        //Add background color to every other row.
        if ((i % 2) == 0) {
            tr.setBackgroundColor(Color.parseColor("#EEEEEE"));
        }

        tableFC.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

    }

1 个答案:

答案 0 :(得分:0)

您不需要readonly。这适用于您不希望用户在字段中输入任何内容的情况。在您的情况下,您只想限制输入的开始

通过使用类和数据属性,您可以在不重复JavaScript代码的情况下在多个页面上重复使用代码。

您需要两个功能 - 一个用于更改select时的功能,另一个用于更改input时的功能。如果您使用$(document).on(),它将监控静态和动态输入。此外,使用'input'监控输入的所有更改,包括复制和粘贴。

$(document).on('change', '.prefix-select', function(){
  var prefix = $(this).find(':selected').data('prefix');
  $(this).closest('form').find('.prefix-input').data('prefix', prefix).val(prefix);
});
$(document).on('input', '.prefix-input', function(){
  var prefix = $(this).data('prefix');
  if($(this).val().lastIndexOf(prefix, 0) !== 0){
    $(this).val(prefix);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <select class="prefix-select">
    <option data-prefix="">Please Select</option>
    <option value="84" data-prefix="+30">Greece</option>
  </select>
  <div>
    Phone
    <input type="tel" name="phone" class="prefix-input">
  </div>
  <div>
    Fax
    <input type="tel" name="fax" class="prefix-input">
  </div>
</form>

JSFiddle example