通用的方法来获取输入的值,而不管类型 [英] Generic way to get the value of an input regardless of type

查看:125
本文介绍了通用的方法来获取输入的值,而不管类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个通用函数,我可以使用jQuery验证插件,根据另一个字段的值创建一个字段。这是我想要发生的事情:




  • 如果字段1的值位于指定的值数组中(当前正在使用否 / a和0),或者是空的,什么都不做。否则,需要使用字段2.



获取字段1的值是问题所在。我用文本类型或< select> 输入来解决这个问题没有问题,但我试图让它与收音机一起工作并且有困难。这里是我的代码摘录:

  var value = $('[name =option]')。val ),
empty = ['no','','n / a','0'];

//如果输入不是空,则需要额外的字段
if($ .inArray(value.toLowerCase(),empty)< 0){// //返回-1如果找不到
required = true;
} else {
required = false;

$ / code $ / pre

这适用于我需要的一切,除了收音机,因为它似乎读取第一个广播的值,不管它是否被检查。

触发required的字段将是各种文本输入之一( text url email 等),a < select> ,或一组选择的无线电。没有多重选择。我将在我的jquery验证配置中将这个函数作为参数传递给 required ,以用于每个我希望应用的字段。获得评估的其他字段的名称属性将可用。



这是我的演示版远,有点丑,但有笔记: http://jsfiddle.net/uUdX2/3/



我试过一堆使用 is(':checked'):已检查选择器,但它们都失败了。我将它们从演示中删除了,因为它们不起作用。



我需要如何使用收音机 text-type才能使用它,或者选择投入,而不知道它会是哪种投入? 试试这个

  var value = $('[name =option]'); 
var type = value.attr(type);

if(type&& type.toLowerCase()=='radio')
value = value.filter(:checked)。val();
else
value = value.val();

工作演示


I'm trying to write a generic function I can use with the jquery validation plugin that will make a field required based on the value of another field. Here's what I want to happen:

  • If Field 1's value is in a specified array of values (currently testing with "No", "n/a", and "0"), or is empty, do nothing. Otherwise, make Field 2 required.

Getting the value of Field 1 is the issue. I had no problem figuring this out with a text-type or <select> input, but I'm trying to get it to work with radios and having difficulty. Here is an excerpt from my code:

var value = $('[name="option"]').val(),
    empty = ['no', '', 'n/a', '0'];

// If the input is not "empty", make the additional field required
if ($.inArray(value.toLowerCase(), empty) < 0) { // returns -1 if not found
    required = true;
} else {
    required = false;
}

This works for everything I need it to except radios, because it seems to read the value of the first radio, regardless of if it was checked or not.

The field that will trigger the "required" will either be one of the various text inputs (text, url, email, etc.), a <select>, or a single choice set of radios. No multiple choice. I'll be passing this function as a parameter to required in my jquery validation config for each field I want it to apply to. The name attribute of the "other" field that gets evaluated will be available to it.

Here's my demo so far, kind of ugly but there are notes: http://jsfiddle.net/uUdX2/3/

I've tried a bunch of different ways using is(':checked') and the :checked selector, but they have all failed. I removed them from the demo because they didn't work.

What do I need to get this working with radios and text-type or select inputs, without knowing which kind of input it will be?

解决方案

Try this

var value = $('[name="option"]');
var type = value.attr("type");

if(type && type.toLowerCase() == 'radio')
  value = value.filter(":checked").val();
else
  value = value.val();

Working demo

这篇关于通用的方法来获取输入的值,而不管类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆