I have an Id City on page which is basically a model property. I want to set it as null. I am trying this to achieve it:
$("#City").val(null);
But it is not setting the value to null.
I have an Id City on page which is basically a model property. I want to set it as null. I am trying this to achieve it:
$("#City").val(null);
But it is not setting the value to null.
您没有将元素值设置为null
,因为 HTML 没有 等的概念null
,undefined
您可以删除该属性,或者更好的是,将其设置为空字符串:
$("#City").val("");
使用普通的javascript,尝试使用setAttribute
方法,
document.getElementById("City").setAttribute("value" , "");
或不使用该setAttribute
方法,
document.getElementById("City").value = "";
jQuery
prop()
您可以使用以下attr()
方法删除 Id :
$('#City').prop('id', null);
或像这样:
$('#City').attr('id', null);
以防万一 :