如果if语句不起作用,则将输入值与else比较,该怎么办?

时间:2019-01-05 10:50:10

标签: jquery if-statement input

我只是做了一个表格,将value变成变量,并试图将它们之间进行比较,但是什么也没发生。

我试图用number()和parseInt将变量转换为整数,但是没有用。

我试图在函数外部声明变量,但没有用

$('#calculate').click(function(){
  var bill = $('#bill').val();
  var serveuse = $("#myselect option:selected").val();
  var sangs = $('#gars').val();
  if(bill===0){
    alert('something');
  }
  else if (bill<10){
    alert('somethinge else');
  }
  else(gars>10 &&bill>20){
    alert('something else else');
  }
})

我的from中有三个输入,“ sangs”和“ bill”是经典输入类型编号,“ serveuse”是一个选择选项输入。我只想将这三个变量与其他if语句进行比较以输出结果。

2 个答案:

答案 0 :(得分:0)

可能import React, { PureComponent } from 'react'; import { Field, reduxForm } from 'redux-form'; import { connect } from 'react-redux'; import { Button } from 'reactstrap'; import axios from 'axios'; import Select from 'react-select'; import { Redirect } from 'react-router-dom'; import PropTypes from 'prop-types'; import EyeIcon from 'mdi-react/EyeIcon'; import Logo from '../../shared/img/logo/logo_light_2.svg'; import * as serverActionTypes from '../../redux/actions/serverActions'; class LogIn extends PureComponent { static propTypes = { handleSubmit: PropTypes.func.isRequired, }; constructor(props) { super(props); this.state = { showPassword: false, in: false, }; } componentDidMount() { localStorage.clear(); } . . . . . handleEmailChange(event) { this.setState({ email: event.target.value }) } handlePasswordChange(event) { this.setState({password: event.target.value}) handleChange = (selectedOption) => { console.log("$$$$$$$$$$$$"); console.log(selectedOption.value); console.log(this.props) if (selectedOption.value === serverActionTypes.devServer().type) { console.log("handle dev change"); {this.props.onSelectDevServerAddress} } else if (selectedOption.value === serverActionTypes.stageServer().type) { console.log("handle stage change"); {this.props.onSelectStageServerAddress} } else if (selectedOption.value === serverActionTypes.prodServer().type) { console.log("handle prod change"); {this.props.onSelectProdServerAddress} } }; render() { const { handleSubmit } = this.props; if (this.state.in === true) { return <Redirect to={{pathname: "/dashboard"}} />; } return ( <div className="account"> <div className="account__wrapper"> <div className="account__card" align="center"> <img src={Logo} alt="example-logo" height="35" style={{marginBottom: '50px'}} /> </div> <div className="account__card"> <form className="form form--horizontal" onSubmit={handleSubmit}> <div className="form__form-group"> <span className="form__form-group-label">E-mail</span> <div className="form__form-group-field"> <Field name="email" component="input" type="email" placeholder="example@mail.com" value={this.state.email} onChange={this.handleEmailChange.bind(this)} /> </div> </div> <div className="form__form-group"> <span className="form__form-group-label">Password</span> <div className="form__form-group-field"> <Field name="password" component="input" type={this.state.showPassword ? 'text' : 'password'} placeholder="Password" value={this.state.password} onChange={this.handlePasswordChange.bind(this)} /> <button className={`form__form-group-button${this.state.showPassword ? ' active' : ''}`} onClick={e => this.showPassword(e)} ><EyeIcon /> </button> </div> </div> <div className="form__form-group"> <span className="form__form-group-label">Server</span> <div className="form__form-group-field"> <div className="form__form-group-input-wrap"> <Select name='server_address_selector' value='prod.example.com' onChange={this.handleChange} options={[ { value: 'dev.example.com', label: 'Dev' }, { value: 'stage.example.com', label: 'Stage' }, { value: 'prod.example.com', label: 'Prod' }, ]} clearable={false} className="form__form-group-select" /> </div> </div> </div> <Button color="success" onClick={this.onLogin.bind(this)} outline> Sign In </Button> <Button color="success" onClick={this.props.onSelectDevServerAddress} outline> DEV </Button> <Button color="success" onClick={this.props.onSelectStageServerAddress} outline> STAGE </Button> <Button color="success" onClick={this.props.onSelectProdServerAddress} outline> PROD </Button> </form> </div> </div> </div> ); } } const mapStateToProps = state => { return { ipAddress: state.server } } const mapDispatchToProps = dispatch => ({ onSelectDevServerAddress: () => dispatch(serverActionTypes.devServer()), onSelectStageServerAddress: () => dispatch(serverActionTypes.stageServer()), onSelectProdServerAddress: () => dispatch(serverActionTypes.prodServer()), }); LogIn = connect( mapStateToProps, mapDispatchToProps )(LogIn); export default reduxForm({ form: 'log_in_form' })(LogIn); 必须是else(gars>10 &&bill>20)

gars 必须是 sangs

答案 1 :(得分:0)

$('#bill').val()将为您提供一个字符串结果,例如"0"

`"0" !== 0`

因此您需要先将其转换为数字,例如

var bill = $('#bill').val() * 1;

或者,使用==会自动转换:"0" == 0


其他问题:

var sangs = ...
if (gars > ...

if () { } else if () { } else () {} 

缺少else

您也有很大一部分缺少结果,因此您可能需要没有else子句的最终if(例如,如果bill == 11,那么您将不会收到警报)