简体   繁体   English

我可以在HTML编号输入字段中使用+ - 而不是箭头吗?

[英]Can I use +- instead of arrows in HTML number input field?

Is it possible to change the arrows in my number input field and use +- signs or images instead? 是否可以更改数字输入字段中的箭头并使用+ - 符号或图像代替?

<label>Quantity</label>
<input id="quantity" type="number" name="quantity" value="1"  min="1" class="tc item-quantity" />

No you cant. 不,你不能。 For the same reason as you cant control the UI on a mobile phone for inputs like a select. 出于同样的原因,你无法控制手机上的用户界面输入,如选择。 The x to remove the text in the focussed element isn't editable either. 删除焦点元素中的文本的x也不可编辑。

Simple make your own (text) input and add two buttons nexto it. 简单地制作您自己的(文本)输入并添加两个按钮。 The pro side to that is that not all browsers support the type=number that well, so it would make for a fine crossbrowser solution 专业方面的是,并非所有浏览器都支持type = number,因此它将成为一个优秀的crossbrowser解决方案


Small example using native JS (made it real quick, so might not fit perfectly): 使用原生JS的小例子(快速实现,因此可能不太适合):

<input id="example" type="text" value="1337" />
<div id="add">Add 1</div>
<div id="subtract">subract 1</div>

And this would be the javascript. 这将是javascript。 If you include this in an external file (which you should) don't forget to place it in some document=ready code, or include it at the bottom of the page 如果将其包含在外部文件中(您应该),请不要忘记将其放在某个document = ready代码中,或者将其包含在页面底部

var input = document.getElementById('example');

document.getElementById('add').onclick = function(){
    input.value = parseInt(input.value, 10) +1
}

document.getElementById('subtract').onclick = function(){
    input.value = parseInt(input.value, 10) -1
}

And here is a jsFiddle.net Demo 这是一个jsFiddle.net演示

see this fiddle and see whether its what u need: 看到这个小提琴,看看它是否需要:

$(window).on('keypress', function(event ){
    if(event.which == 43){
       var value =  $('#quantity').val();
       $('#quantity').val(parseInt(value)+1);
        return false;
    }else if(event.which == 45){
         var value =  $('#quantity').val();
       $('#quantity').val(parseInt(value)-1);
        return false;
    }
});

http://jsfiddle.net/abhiklpm/7TUe2/ http://jsfiddle.net/abhiklpm/7TUe2/

Edit: Updated the fiddle using images 编辑:使用图像更新小提琴

$('.plus').click(function(){
        var value =  $('#quantity').val();
       $('#quantity').val(parseInt(value)+1);
});
$('.minus').click(function(){
        var value =  $('#quantity').val();
       $('#quantity').val(parseInt(value)-1);
});

http://jsfiddle.net/abhiklpm/7TUe2/1/ http://jsfiddle.net/abhiklpm/7TUe2/1/

I am pretty sure this is browser specific, since each one renders this input differently. 我很确定这是特定于浏览器的,因为每个都以不同的方式呈现此输入。 If you really need that type of control, I would suggest a JavaScript form field wrapper, that can allow you to style each UI in a cross-browser way. 如果您确实需要这种类型的控件,我建议使用JavaScript表单字段包装器,它允许您以跨浏览器的方式设置每个UI的样式。

I ended up using the comment from @Joe. 我最终使用了@Joe的评论。 "Here's a quick version I just made: jsfiddle.net/mLyX2/1 - it's not the best but it should at least give you an idea of how to do it." “这是我刚刚制作的快速版本:jsfiddle.net/mLyX2/1 - 它不是最好的,但至少应该让你知道如何做到这一点。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM