首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果单选按钮是真还是假,如何输入到Firebase数据库?

如果单选按钮是真还是假,如何输入到Firebase数据库?
EN

Stack Overflow用户
提问于 2017-12-07 19:42:21
回答 1查看 335关注 0票数 0

我已经成功地将所有文本框输入推入Firebase,但是当我对单选按钮使用相同的方法时,我得到的结果是:"Uncaught : Reference.push failed:第一个参数包含属性‘Tires.runflat’中的未定义的参数“,我不知道任何变量是如何定义的,所以我对此非常困惑,尽管我确信这是简单的事情。

html:

代码语言:javascript
运行
复制
<body>
<div class="container">
<div class="content" style="background-color: white;">
    <div id="heading">
        <h1>Add A Tire<h1>
    </div>
    <form id="tires">
        <div id="1" class="form-sections">
            <div class="input-div">
            <label for="width" class="label">Width</label>
            <select name="width" id="width" type="select"  class="input">
                <option value="">---</option>
                        <?php foreach ($tireWidths as $width) {?>
                            <option value="<?php echo $width;?>"><?php echo $width;?></option>
                        <?php } ?>

            </select></div>
            <div class="easy-div">
                <div>
                            <?php foreach ($width100s as $width){ ?>
                                <z data-role="button" data-val="<?php echo $width;?>" class="width-sm"><?php echo $width?></z>
                            <?php } ?>

                            </div>
                        <div>
                            <?php foreach ($width200s as $width){ ?>
                                <z data-role="button" data-val="<?php echo $width;?>" class="width-sm"><?php echo $width?></z>
                            <?php } ?>

                        </div>
                        <div>
                    <?php foreach ($width300s as $width){ ?>
                                <z data-role="button" data-val="<?php echo $width;?>" class="width-sm"><?php echo $width?></z>
                            <?php } ?>
                        </div>
                </div>
        </div> <!-- closing div#1-->
        <div id="2" class="form-sections">
            <div class="input-div">
                <label for="aspect" class="label">Aspect Ratio</label>
                <select class="input"  name="aspect" id="aspect">
                            <option value="">---</option>    
                    <?php foreach ($aspects as $aspect) {?>
                                    <option value="<?php echo $aspect;?>"><?php echo $aspect;?></option>
                                <?php   }?>

                        </select>
            </div>
            <div class="easy-div">
                <div>
                                <?php foreach ($aspectsTop as $a){ ?>
                                <z data-role="button" data-val="<?php echo $a;?>" class="aspectVal"><?php echo $a;?></z>
                            <?php } ?>
                        </div>
                        <div>
                            <?php foreach ($aspectsBottom as $a){ ?>
                            <z data-role="button" data-val="<?php echo $a;?>" class="aspectVal"><?php echo $a;?></z>
                            <?php } ?>
                        </div>
                </div>
    </div> <!-- closing div#2-->
        <div id="3" class="form-sections">
            <div class="input-div">
                <label class="label" for="rim">Rim Diameter</label>
                <select id="rim" name="rim" class="input">
                            <option value="">---</option>
                            <?php foreach ($rimDiameter as $rim){ ?>
                            <option value="<?php echo $rim;?>"><?php echo $rim;?></option>
                            <?php } ?>
                        </select>
            </div>
            <div class="easy-div">
                <div>
                            <?php foreach ($rimDiaTop as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="rimVal"><?php echo $r;?></z>
                            <?php }?>
                    </div>
                    <div>
                            <?php foreach ($rimDiaBottom as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="rimVal"><?php echo $r;?></z>
                            <?php }?>
                    </div>
            </div>
        </div> <!-- closing div#3-->
        <div id="4" class="form-sections">
            <div class="input-div">
                <label class="label" for="manufacturer">Manufacturer</label>
                <select class="input" name="manufacturer" id="manufacturer">
                        <option value="">---</option>
                        <?php foreach ($manufacturers as $m) { ?>
                        <option value="<?php echo $m;?>"><?php echo $m;?></option>
                        <?php } ?>
                    </select>
            </div>
            <div class="easy-div">
                    <div id="manu-group-one">
                            <?php foreach ($manufacturersOne as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="manVal"><?php echo $r;?></z>
                            <?php }?>
                        </div>
                        <div id="manu-group-two">
                            <?php foreach ($manufacturersTwo as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="manVal"><?php echo $r;?></z>
                            <?php }?>
                        </div>
                        <div id="manu-group-three">
                            <?php foreach ($manufacturersThree as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="manVal"><?php echo $r;?></z>
                            <?php }?>
                        </div>
                        <div id="manu-group-four">
                            <?php foreach ($manufacturersFour as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="manVal"><?php echo $r;?></z>
                            <?php }?>
                        </div>
                        <div id="manu-group-five">
                            <?php foreach ($manufacturersFive as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="manVal"><?php echo $r;?></z>
                            <?php }?>
                        </div>
                        <div id="manu-group-six">
                            <?php foreach ($manufacturersSix as $r) { ?>
                            <z data-role="button" data-val="<?php echo $r;?>" class="manVal"><?php echo $r;?></z>
                            <?php }?>
                        </div>
            </div>
        </div> <!-- closing div#4-->
        <div id="5" class="form-sections">
            <div id="5a" class="inner-B"><div class="input-div"><label class="label" for="model">Model</label>
            <select id="model" name="model"  class="input">
                <option value="">---</option>   
                <option value="test">Test</option>
            </select>
            </div>
            <div class="easy-div">EASY BUTTONS</div>
            </div><!--closing A -->

            <div id="5B" class="inner-A"><label class="label" for="weight">Shipping Weight</label><input type="text" name="weight" id="weight" class="input"></div><!--closing B -->
        </div> <!-- closing div#5-->
        <div id="6" class="form-sections">
            <div id="6A" class="inner-C">
                <div class="input-div">
                    <label class="label" for="trim">Trim</label>
                    <select name="trim" id="trim" class="input" >
                        <option value="">---</option>
                        <option value="test">Test</option>
                    </select>
                </div>
                <div class="easy-div">EASY BUTTONS</div>
            </div><!--closing A -->
            <div id="6B" class="inner-D">
                <label class="label" for="cost">Tire Cost</label>
                <input name="cost" id="cost" class="input">
            </div><!--closing B -->
            <div id="6C" class="inner-E">PICTURES
            <div class="pix">

            <input id="browse" type="file" onchange="previewFiles()" multiple>
            <div id="preview"></div>

            </div>
            </div><!--closing C -->
        </div> <!-- closing div#6-->
        <div id="7" class="form-sections">
            <div id="7A" class="inner-D">
                <div class="label">Run Flat?</div>
                <div class="input"><input type="radio" id="runflat" name="runflat" value="yes"> Yes  <input type="radio" name="runflat" value="no"> No </div>
            </div><!--closing A -->
            <div id="7B" class="inner-F">
                <div class="label">Tread Depth</div>
                <div class="input">
                    <input type="text" name="tread" id="tread" ><br><br>
                    <?php for($i=1; $i<11; $i++){ ?>
                        <z data-role="button" class="treadDepthVal"> <?php echo $i;?></z>
                    <?php } ?>
                    <?php for($i=11; $i<21; $i++){ ?>
                        <z data-role="button" class="treadDepthVal" data-val="<?php echo $i; ?>"> <?php echo $i;?></z>
                    <?php } ?>    <br><z data-role="button"  class="treadDepthVal" data-val=".25">.25 </z>
                    <z data-role="button" class="treadDepthVal"  data-val=".5">.5</z>
                    <z data-role="button" class="treadDepthVal" data-val=".75">.75</z>
                </div>
            </div><!--closing B -->
            <div id="7C" class="inner-F"><div class="label">DOT Date</div><div class="input"><input type="text" name="dotdate" id="dotdate" required><br>
            <?php for($i=0; $i <10;  $i++){ ?>
                <z data-role="button" class="dotDateVal" data-val="<?php echo $i; ?>"><?php echo $i;?></z>
                <?php } ?>
            </div></div><!--closing C -->
            <div id="7D" class="inner-F"><label class="label" for="patches">Number of Patches</label>
            <div class="input">
            <input type="text" name="patches" id="patches" required><br>
            <?php for($i=0;$i<5;$i++){?>
                <z data-role="button" class="patchVal" data-val="<?php echo $i; ?>" maxlength="4"><?php echo $i;?></z>
                <?php
            }?></div></div> <!--closing D-->    
        </div> <!-- closing div#7-->
        <div id="8" class="form-sections">
            <div id="8A" class="inner-G"><div class="label">Retail Price</div>
                <input type="text" id="retailPrice" name="retailPrice" class="input" required>
            </div>
            <div id="8B" class="inner-G"><div class="label">C.U.T. Share</div><div class="input">- $4.99 </div></div>
            <div id="8C" class="inner-G"><div class="label">C.C. Fee</div><div class="input"><?php echo $ccfee;?></div></div>
            <div id="8D" class="inner-G">Not Applicable</div>
            <div id="8E" class="inner-G"><div class="label">Shipping Est.</div><div class="input"><?php echo $shipEst;?></div></div>
            <div id="8F" class="inner-G"><div class="label">My Pay Out</div><div class="input"><?php echo $payOutRetail;?></div></div>
            <div id="8G" class="inner-G"><div class="label">Tire NOT listed on Cheapest Used Tires</div><div class="input"><input type="checkbox"
name="notListed" id="notListed"></div></div>

        </div> <!-- closing div#8-->
        <div id="9" class="form-sections">
            <div id="9A" class="inner-G"><div class="label">Wholesale Price</div>
            <input type="text" id="retailPrice" name="retailPrice" class="input" required></div>
            <div id="9B" class="inner-G"><div class="label">C.U.T. Share</div><div class="input">- $4.99</div></div>
            <div id="9C" class="inner-G"><div class="label">C.C. Fee</div><div class="input"><?php echo $ccfee;?></div></div>
            <div id="9D" class="inner-G"><div class="label">Buyer&apos;s Bucks</div><div class="input">- $3.00</div></div>
            <div id="9E" class="inner-G"><div class="label">Shipping Est.</div><div class="input"><?php echo $shipEst;?></div></div>
            <div id="9F" class="inner-G"><div class="label">My Pay Out</div><div class="input"><?php echo $payOutWS;?></div></div>
            <div id="9G" class="inner-G"><div class="label">Wholesale ONLY</div><div class="input"><input type="checkbox" name="notListed" id="notListed"></div></div>
        </div><!--closing div#9-->
    <input type="submit" id="submit" value="Add This Tire">
    </form>
    </div>
</div> <!-- closing container -->

<!-- scripts -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
<script src="/Emily-Projects/auth/tire/new-add-a-tire.js"></script>

javascript:

代码语言:javascript
运行
复制
var config = {
apiKey: "AIzaSyAjzWcrR4nvdb4BtP1kMeBPx_-lGeYVkAg",
authDomain: "cutdb-41c5d.firebaseapp.com",
databaseURL: "https://cutdb-41c5d.firebaseio.com",
projectId: "cutdb-41c5d",
storageBucket: "cutdb-41c5d.appspot.com",
messagingSenderId: "768551466581"
};
firebase.initializeApp(config);

firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
     //Add a Tire to Firebase
//vars for elements/inputs
var width = document.getElementById("width");
var aspect = document.getElementById("aspect");
var rim = document.getElementById("rim");
var manufacturer = document.getElementById("manufacturer");
var model = document.getElementById("model");
var weight = document.getElementById("weight");
var trim = document.getElementById("trim");
var cost = document.getElementById("cost");
var runflat = document.getElementById("runFlat")
var tread = document.getElementById("tread"); 
var dotdate = document.getElementById("dotdate");
var patches = document.getElementById("patches");
var retailPrice = document.getElementById("retailPrice");
var wholesalePrice = document.getElementById("wholesalePrice");
var notListed = document.getElementById("notListed");
var wholesaleOnly = document.getElementById("wholesaleOnly");

//RYDER PLEASE
//insert the pictures into the DB
//make a variable that accesses the pictures input
//insert the radio button value into the DB (input name: runflat)
//make a variable that accesses the radio button
//make the "tread depth" decimal buttons (.25, .5, and .75) append to the 
number chosen from whole number buttons (1-20) to all insert into single 
cell of the table and show in the input#tread
//make a radio button required
//figure out how to append the four inputs of the DOT DATE together to make 
one 4-digit number and show in the input#dotdate
//checkbox inputs for notListed, wholesaleOnly

//easy buttons
//classes : width-sm, aspectVal, rimVal, manVal, modelVal, trimVal, treadDepthVal, dotDateVal, patchVal


$('.width-sm').click(function(){ 
        $(width).val($(this).data('val')).trigger('change');
})
$('.aspectVal').click(function(){ 
        $(aspect).val($(this).data('val')).trigger('change');
})
$('.rimVal').click(function(){ 
        $(rim).val($(this).data('val')).trigger('change');
})
$('.manVal').click(function(){ 
        $(manufacturer).val($(this).data('val')).trigger('change');
})
$('.modelVal').click(function(){ 
        $(model).val($(this).data('val')).trigger('change');
})
$('.trimVal').click(function(){ 
        $(trim).val($(this).data('val')).trigger('change');
})

//MAKE WORK
$('.patchVal').click(function(){ 
        $(patches).val($(this).data('val')).trigger('change');
})
//APPEND THESE???
$('.treadDepthVal').click(function(){ 
        $(tread).val($(this).data('val')).trigger('change');
})
$('.dotDateVal').click(function(){ 
        $(dotdate).val($(this).data('val')).trigger('change');
})

//btns
var submitBtn = document.getElementById("submit");
//Function: submitClick();

var form = document.getElementById("tires");

$(form).on('submit', event => {
event.preventDefault();
window.alert("works");

const w = $(width).val();
const a = $(aspect).val();
const r = $(rim).val();
const m = $(manufacturer).val();
const mo = $(model).val();
const sw = $(weight).val();
const t = $(trim).val();
const c = $(cost).val();
const rf = $(runflat).val();
const td = $(tread).val();
const d = $(dotdate).val();
const p = $(patches).val();
const rp = $(retailPrice).val();
const wp = $(wholesalePrice).val();

firebase.database().ref('/Tires').push({
    "width": w,
    "aspect": a,
    "rim": r,
    "manufacturer": m,
    "model": mo,
    "weight": sw,
    "trim": t,
    "cost": c,
    "runflat": rf,
    "tread": td,
    "dotdate": d,
    "patches": p,
    "retailprice": rp,
    //"wp": wp

});

});


//picture code
var j = 0;

function previewFiles() {

if (j < 9) {


    j++;
    alert(j);
    var preview = document.querySelector('#preview');
    var files = document.querySelector('input[type=file]').files;

    function readAndPreview(file) {

        // Make sure `file.name` matches our extensions criteria
        if (/\.(jpe?g|png|gif)$/i.test(file.name)) {
            var reader = new FileReader();

            reader.addEventListener("load", function() {
                var image = new Image();
                image.height = 100;
                image.title = file.name;
                image.src = this.result;
                preview.appendChild(image);
            }, false);

            reader.readAsDataURL(file);
        }

    }

    if (files) {
        [].forEach.call(files, readAndPreview);
    }


} else {
    alert("I want to get in here");
    document.getElementById("browse").disabled = true;
}
}



 } else {
  // No user is signed in.
  alert("User is not Signed In");
   }
   });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-10 03:10:18

这句话:

代码语言:javascript
运行
复制
var runflat = document.getElementById("runFlat")

应改为:

代码语言:javascript
运行
复制
var runflat = document.getElementById("runflat")

这是因为JavaScript中的in区分大小写。

顺便提一句,我认为const rf = $(runflat).val();这条线行不通。每次只需将rf设置为"on"即可。更正确的方法是这样做:

代码语言:javascript
运行
复制
const rf = $("input[id='runflat']:checked").val();

如果选中单选按钮,这将将rf设置为"on",如果不是,则设置为undefined。如果要更改这些值,可以使用Elvis运算符:

代码语言:javascript
运行
复制
const rf = $("input[id='runflat']:checked").val() ? "yes" : "no";

现在,如果选中单选按钮,rf将被设置为"yes",如果不是,则设置为"no"

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47702616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档