JQuery用DropDownList替换文本框

在选择不同的DropDownList中的项目后,我想用DropdownList更改页面上的文本框(除此之外,更改发生的是bool是真的)。

$('#InvoiceNumber').replaceWith($(@Html.DropDownList("CategoryId", new SelectList(Model.Categories, "Value", "Text"), "Choose a category"))); 

但发生以下错误:

Uncaught SyntaxError:意外的标识符

这是为什么会发生的?

在您的replaceWith()您使用Html.DropDownList()帮助器方法生成已使用jQuery选择器$()包装的HTML。

jQuery选择器看起来是多余的:

 $('#InvoiceNumber').replaceWith(@Html.DropDownList("CategoryId", new SelectList(Model.Categories, "Value", "Text"), "Choose a category")); 

试试这个:

  $('#InvoiceNumber').replaceWith($('@Html.DropDownList("CategoryId", new SelectList(Model.Categories, "Value", "Text"), "Choose a category")')); 

这也应该有效:

  $('#InvoiceNumber').replaceWith('@Html.DropDownList("CategoryId", new SelectList(Model.Categories, "Value", "Text"), "Choose a category")');