根据无线电按钮选择JavaScript中的值

发布于 2025-02-11 10:05:33 字数 1783 浏览 0 评论 0原文

在我的方案中,我想使用无线电按钮选择更改表值。

无线电按钮的代码

@Html.RadioButtonFor(m => m.Doctype1, new {
  id = "doctype1", @onclick = "DocTypeSelect()"
}) @Html.Label("largedocs")

@Html.RadioButtonFor(m => m.Doctype2, new {
  id = "doctype2", @onclick = "DocTypeSelect()"
}) @Html.Label("SmallDocs")

@Html.RadioButtonFor(m => m.Doctype3, new {
  id = "doctype3", @onclick = "DocTypeSelect()"
}) @Html.Label("MediumDocs")

@Html.RadioButtonFor(m => m.Doctype4, new {
  id = "doctype4", @onclick = "DocTypeSelect()"
}) @Html.Label("VerySmallDocs")

我为隐藏其他下拉值的逻辑编写了逻辑。通过使用我可以更改下拉列表并隐藏其他3个按钮。


 <td id="DocTypes1">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })

 <td id="DocTypes2">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })

 <td id="DocTypes3">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })

 <td id="DocTypes4">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })


在JavaScript中,如果选择了一个特定的值,我想获得该值并在格式的表视图中显示。

现有代码始终仅获得一种DOC值。我不知道为什么???


funtion ChangeTypes(){
var docId=$("SelectDocType").val();
var docType=$("SelectDocType option:selected").text();
if(SelectDocType !="")
{

//my logic

}

实际上,当用户更改下拉值时,我需要获取数据。 我解决这个问题。

In my scenario, I want to change the value of a table by using radio button selection.

Code for radio button

@Html.RadioButtonFor(m => m.Doctype1, new {
  id = "doctype1", @onclick = "DocTypeSelect()"
}) @Html.Label("largedocs")

@Html.RadioButtonFor(m => m.Doctype2, new {
  id = "doctype2", @onclick = "DocTypeSelect()"
}) @Html.Label("SmallDocs")

@Html.RadioButtonFor(m => m.Doctype3, new {
  id = "doctype3", @onclick = "DocTypeSelect()"
}) @Html.Label("MediumDocs")

@Html.RadioButtonFor(m => m.Doctype4, new {
  id = "doctype4", @onclick = "DocTypeSelect()"
}) @Html.Label("VerySmallDocs")

I wrote a logic for hide other dropdown values. By using I'm able to change the dropdown and hiding other 3 buttons.


 <td id="DocTypes1">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })

 <td id="DocTypes2">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })

 <td id="DocTypes3">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })

 <td id="DocTypes4">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes()"
    })


In Javascript, If a particular value is selected I want to get that value and and display in a table view of format.

The existing code is always getting the one type of doc value only. I don't know why???


funtion ChangeTypes(){
var docId=$("SelectDocType").val();
var docType=$("SelectDocType option:selected").text();
if(SelectDocType !="")
{

//my logic

}

actually I need to get the data when a user change the dropdown values.
Help me to solve this..

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独留℉清风醉 2025-02-18 10:05:33

如果您需要找到所有下拉列表,则应:

$('select[id^="SelectDocType"]')

应该给您所有下拉列表ID的匹配项,尽管它不是元素共享相同ID的最佳练习,如果您可以给他们一个唯一的ID,则可以找到它们,那么您可以找到它们所有使用类名称都

$('.myclass')

需要用于每个循环的所有名称,然后将每个元素项目的值从每个元素中获取

$( "select" ).each(function( index ) {
    console.log( index + ": " + $( this ).val() );
})

,但是如果您想让自己更轻松地生活,为什么不将价值传递给函数:

 <td id="DocTypes2">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes(this.value)"
    })
...
funtion ChangeTypes(val){
   console.log(val);
}

我希望这会有所帮助

if you need to find all the dropdowns then:

$('select[id^="SelectDocType"]')

should give you all the dropdowns whos id's match, although its no best practice for elements to share the same id, if you can give them a unique id abut the same class then you can find them all using the class name

$('.myclass')

you will need to use a for each loop then to get the values out of each element item

$( "select" ).each(function( index ) {
    console.log( index + ": " + $( this ).val() );
})

but if you want to make life easier for yourself why not pass the value to the function like so:

 <td id="DocTypes2">
    @Html.DropDownlistfor(m => m.Doctype, new selectlist(m.BigDocs, "Value", "Text", m.BigDocs), new {
      @id = "SelectDocType", 
      @onchange = "ChangeTypes(this.value)"
    })
...
funtion ChangeTypes(val){
   console.log(val);
}

I hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文